package com.xingli.jingdongshop.controller;

import com.alibaba.fastjson.JSON;
import com.google.protobuf.InvalidProtocolBufferException;
import com.xingli.jingdongshop.beans.ShopReqCmdEnums;
import com.xingli.jingdongshop.proto.MsgJinDouShop;
import com.xingli.jingdongshop.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/jd/shop")
public class ShopController {
    @Autowired
    private GoodsCotroller goodsCotroller;

    @Autowired
    private RedeemController redeemController;

    private String verifyAppKey = "dsfasdtcxv88!0stponxfa=";

    //    CMD_UPDATE_SHOP = "cmd_update_shop",            --请求商店配置
//            CMD_UPDATE_STATUS = "cmd_update_redeem_status", --请求更新兑换码状态
//            CDM_OBTAIN_REDEEM = "cmd_obtain_redeem",
    //金豆商店相关
    @PostMapping(value = "/jd")
    public Object getShop(@RequestBody String test) throws InvalidProtocolBufferException, UnsupportedEncodingException {
        test = URLDecoder.decode(test, "UTF-8");
        if (test.substring(test.length()).equals("=")) {
            test = test.substring(0, test.length() - 1);
        }
        System.out.println(test);
        ShopReqCmdEnums reqCmd = ShopReqCmdEnums.valueOf(JSON.parseObject(test).getString("req_cmd"));
        String paramData = JSON.parseObject(test).getString("param_data");
        String svr_name = JSON.parseObject(test).getString("svr_name");
        int req_time = Integer.parseInt(JSON.parseObject(test).getString("req_time"));
        String verifyCode = String.format("cmd=%s&svr_name=%s&time=%d%s", JSON.parseObject(test).getString("req_cmd"),
                JSON.parseObject(test).getString("svr_name"), Integer.parseInt(JSON.parseObject(test).getString("req_time")), verifyAppKey);
        verifyCode = CommonUtil.encoderByMd5(verifyCode);
        String authCode = JSON.parseObject(test).getString("auth_code");
        System.out.println("verifyCode " + verifyCode);
        System.out.println("authCode " + JSON.parseObject(test).getString("auth_code"));

        if (verifyCode.equals(authCode)) {
            System.out.println("验证通过");

            String returnAuthCode = String.format("%s&svr_name=%s&time=%dcmd=%s", verifyAppKey,
                    JSON.parseObject(test).getString("svr_name"), Integer.parseInt(JSON.parseObject(test).getString("req_time")), JSON.parseObject(test).getString("req_cmd"));
            Map<String, Object> map = new HashMap<>();
            returnAuthCode = CommonUtil.encoderByMd5(returnAuthCode);
            map.put("auth_code", returnAuthCode);
            switch (reqCmd) {
                case cmd_update_shop:
                    map.put("req_cmd", reqCmd.name());
                    map.put("svr_name", svr_name);
                    map.put("req_time", req_time);
                    map.put("param_data", goodsCotroller.getShop(1));
                    return map;
                case cmd_obtain_redeem:
                    MsgJinDouShop.ReqRedeem reqRedeem = MsgJinDouShop.ReqRedeem.parseFrom(Base64.getDecoder().decode(paramData));
                    map.put("req_cmd", reqCmd.name());
                    map.put("svr_name", svr_name);
                    map.put("req_time", req_time);
                    map.put("param_data", redeemController.getRedeem(reqRedeem.getGoodsId(), reqRedeem.getPlayerId(), reqRedeem.getReqUuid()));
                    return map;
            }
        } else {
            System.out.println("验证失败");
        }

        return null;
    }

    //{"svr_name":"doudou","req_cmd":"cmd_update_shop","req_time":1531126324}=
    public static void main(String[] args) throws UnsupportedEncodingException, InvalidProtocolBufferException {
        String tt = "CAEQARpcCAESDOenr+WIhuS4k+WMuhgCIiMIIRICNTAYASABKDIwZDgAQGRI////////////AVABWABgASIjCCISAjExGAEgCigUMGQ4AEBkSP///////////wFQAVgAYAIaYQgCEgzljaHnsbvkuJPljLoYASIsCB8SAjEwGAEgASgKMP///////////wE4AkBkSP///////////wFQAVgAYAEiHwggEgM1MDIYASACKGMwZDgBQGRI/vqS2gVQAVgAYAE=";
        byte[] b = Base64.getDecoder().decode(tt);
        MsgJinDouShop.JinDouShop m = MsgJinDouShop.JinDouShop.parseFrom(b);
        System.out.println(m.getUpdateTime());


    }

}
